package net.huashitong.supervision.task.service.insideService;

import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.insideDuCha.entity.InsideMainTaskEntity;
import net.huashitong.supervision.insideDuCha.entity.InsideUserTaskEntity;
import net.huashitong.supervision.insideDuCha.filter.InsideMainTaskFilter;
import net.huashitong.supervision.insideDuCha.service.InsideMainTaskService;
import net.huashitong.supervision.insideDuCha.service.InsideUserTaskService;
import net.huashitong.supervision.insideDuCha.util.CommUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 内部督查事项任务
 */
@Service
public class InsideNewServcie {

    @Autowired
    private InsideMainTaskService insideMainTaskService;

    @Autowired
    private InsideUserTaskService insideUserTaskService;



    @Transactional
    public int saveInsideTask(InsideMainTaskFilter filter)
    {
        List<String> selectUsers = filter.getSelectUsers();
        if(ObjectUtils.isBlank(selectUsers))
        {
            return 0;
        }
        InsideMainTaskEntity entity = saveMainTask(filter);
        insideMainTaskService.save(entity,filter.getLoginerId());
        String taskMainId = entity.getSysId();
        for (String userId : selectUsers) {

            InsideUserTaskEntity userTask = saveUserTask(taskMainId, entity.getDeptId(), userId, entity.getRemindDate());
            insideUserTaskService.save(userTask,filter.getLoginerId());
        }
        //发短信；

        return 1;
    }


    /**
     * 保存主任务
     * @param filter
     * @return
     */
    public InsideMainTaskEntity saveMainTask(InsideMainTaskFilter filter)
    {
        InsideMainTaskEntity entity = new InsideMainTaskEntity();
        String taskName = filter.getTaskName();
        String taskCont = filter.getTaskCont();
        int timeType = filter.getTimeType();
        String cycleValue = filter.getCycleValue();
        String deptId = filter.getDeptId();

        entity.setTaskState(1+"");
        entity.setBackType(0+"");
        entity.setTaskName(taskName);
        entity.setTaskCont(taskCont);
        entity.setTimeType(timeType);
        entity.setCycleValue(cycleValue);

        String remindDate = CommUtil.remindDate(timeType, cycleValue);
        entity.setRemindDate(remindDate);
        entity.setDeptId(deptId);
        return  entity;
    }

    /**
     * 人员任务
     * @param taskMainId
     * @param deptId
     * @param userId
     * @param remindDate
     * @return
     */
    public InsideUserTaskEntity saveUserTask(String taskMainId,String deptId,String userId,String remindDate)
    {
        InsideUserTaskEntity entity = new InsideUserTaskEntity();
        entity.setMainTaskId(taskMainId);
        entity.setUserId(userId);
        entity.setDeptId(deptId);
        entity.setTaskState("0");
        entity.setBackType("0");
        entity.setRemindDate(remindDate);
        entity.setIsCycle(0);
        entity.setTimeRound(1);

        return entity;
    }






}
